package io.renren.modules.PCguanli.controller;

import java.io.IOException;
import java.util.*;

import io.renren.modules.PCguanli.entity.ExcelExporter;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.PCguanli.entity.ViolationRecordsEntity;
import io.renren.modules.PCguanli.service.ViolationRecordsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 违规信息
 *
 * @author zhangdanxiaozu
 * @email dan.zhang@zju.edu.cn
 * @date 2024-07-08 19:12:14
 */
@RestController
@RequestMapping("PCguanli/violationrecords")
public class ViolationRecordsController {
    @Autowired
    private ViolationRecordsService violationRecordsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("PCguanli:violationrecords:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = violationRecordsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{recordid}")
    @RequiresPermissions("PCguanli:violationrecords:info")
    public R info(@PathVariable("recordid") Integer recordid){
		ViolationRecordsEntity violationRecords = violationRecordsService.getById(recordid);

        return R.ok().put("violationRecords", violationRecords);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("PCguanli:violationrecords:save")
    public R save(@RequestBody ViolationRecordsEntity violationRecords){
		violationRecordsService.save(violationRecords);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("PCguanli:violationrecords:update")
    public R update(@RequestBody ViolationRecordsEntity violationRecords){
		violationRecordsService.updateById(violationRecords);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("PCguanli:violationrecords:delete")
    public R delete(@RequestBody Integer[] recordids){
		violationRecordsService.removeByIds(Arrays.asList(recordids));

        return R.ok();
    }

    /**
     * 导出
     */
    @RequestMapping("/export/{recordid}")
    @RequiresPermissions("PCguanli:violationrecords:export")
    public R export(@PathVariable("recordid") Integer recordid) {
        ViolationRecordsEntity violationRecordsEntity= violationRecordsService.getById(recordid);
        List<ViolationRecordsEntity> violationRecordsList = new ArrayList<>();
        violationRecordsList.add(violationRecordsEntity);
        String newFileName = UUID.randomUUID().toString()+"data.xslx";
        String excelFilePath = "F:\\project\\"+newFileName;
        ExcelExporter exporter= new ExcelExporter();
        try{
            exporter.exportToExcel(violationRecordsList,excelFilePath);
            System.err.println("打印成功");
        }catch (IOException e){
            System.err.println("导出失败: " + e.getMessage());
            e.printStackTrace();
        }
        return R.ok().put("violationrecords", violationRecordsEntity);
    }


}
